public class Solution990 {
    public boolean equationsPossible(String[] equations) {
        int[] b=new int[26];
        for (int i=0;i<26;i++){
            b[i]=i;
        }
        for (String equation : equations) {
            if (equation.charAt(1)=='='){
                int x=find(b,equation.charAt(0)-'a');
                int y=find(b,equation.charAt(3)-'a');
                b[x]=y;
            }
        }
        for (String equation : equations) {
            if (equation.charAt(1)=='!'){
                int x=find(b,equation.charAt(0)-'a');
                int y=find(b,equation.charAt(3)-'a');
                if (x==y){
                    return false;
                }
            }
        }
        return true;
    }

    private int find(int[] b, int i) {
        while (b[i]!=i){
            i=b[i];
        }
        return i;
    }

    public static void main(String[] args) {
        System.out.println(new Solution990().equationsPossible(new String[]{"a==b","a!=b"}));
    }
}
